var sinceNum=0;
var page = 1;//页数
var total = 0;//总条数
var totalPage = 0;//总页数
var fId="";
$(function () {
    var id = window.location.href.split("id=")[1]//得到传递过来的代表id
    fId=id;
    var contactNum=0;
    var liveNum=0;
    /**
     * tab切换
     * @type {*|string}
     */
    $(".tab_1").eq(0).show();
    $("#tabDb li a").click(function(){
        var num =$("#tabDb li a").index(this);

        $(".tab_1").hide();
        $(".tab_1").eq(num).show();
        $(this).attr("class","cur");
        $(this).parent().siblings().children("a").attr("class"," ");
    });
    //履职信息
    //resumption(id)
    //代表联系选民
    dblx(id);
    //民生实事
    msList(id);

    //查询代表详细信息
    $.ajax({
        url:'./js/digitalPlatform/representative.json',
        type: 'GET',
        dataType: 'JSON',
        async: false,
        success: function (data){
            $.each(data.results,function (index,val) {
                if (val.id==id){
                    //获取姓名
                    var pName = `${val.pName}`;
                    $("#pName").html(pName);
                    //获取图片
                    var picture =val.pPicture;
                    if (picture !=""&&picture !=undefined&&picture !=0){

                    }else{
                        picture="../sjglpthtml/css/images/db_icon.png"
                    }

                    var pPicture = `<img src="`+picture+`"/>`;
                    $("#pPicture").html(pPicture);
                    //获取基本信息
                    var dbInformation = `<ul>
              <li><div><span>姓名</span>：${val.pName}</div><div><span>民族</span>：${val.pNation}</div></li>
              <li><div><span>性别</span>：${val.pSex}</div><div><span>党派</span>：${val.pParty}</div></li>
              <li><div><span>出生年月</span>：${val.pBirth}</div><div><span>学历</span>：${val.pEducation}</div></li>
              <li><div><span>籍贯</span>：${val.pBirthplace}</div><div><span>手机号码</span>：${val.pPhone}</div></li>

                <li><span>单位及职务</span>：${val.pwork}</li>
                <li><span>代表级别</span>：${val.pResp}</li>
                <li><span>所属届次</span>：${val.pDate}</li>
            </ul>`;
                    $("#dbInformation").html(dbInformation);
                }
            })
        }
    });

    // //查询代表联系选民数据
    function dblx(id){
        $.ajax({
            url:'./js/contact/contact.json',
            type: 'GET',
            dataType: 'JSON',
            async: false,
            success: function (data) {
                var contactXM="";
                var head =`<table cellpadding="0" cellspacing="0" border="0" width="100%" class="table_data">
                  <tr>
<!--                      <th width="10%">代表姓名</th>-->
                      <th width="10%">接待走访时间</th>
                      <th width="40%">标题</th>
                      <th width="10%">类别</th>
                      <th width="10%">承办单位</th>
                      <th width="10%">是否解决</th>
                      <th width="10%">操作</th>
                  </tr>`;
                $.each(data.results,function (index,val) {
                    if (val.pId==id){
                        contactNum=contactNum+1;
                        //获取列表基本信息
                        contactXM =contactXM+ `<tr>
<!--                      <td class="align_center">${val.pName}</td>-->
                      <td class="align_center">${val.receiveTime}</td>
                      <td class="align_center">${val.title}</td>
                      <td class="align_center">${val.category}</td>
                      <td class="align_center">${val.organizer}</td>
                      <td class="align_center" >${val.isSolve}</td>
                      <td class="align_center"><a href="javascript:;" onclick="info(${val.id})" ><i class="icon iconfont icon_chakan "></i></a></td>
                  </tr>`;
                    }
                })
                var contact= "";
                if (contactNum&&contactNum!=0){
                    contact= head+contactXM+`</table>`;
                }else {
                    contact=head+`<tr><td colspan="6" style=" text-align:center;">暂无数据</td></tr></table>`
                }
                $("#xm_table").html(contact);
            }
        });
    }
    $("#contactNum").html(contactNum);
    //民生实事
    function msList(id){
        var proName="";
        $.ajax({
            url:'./js/position/position.json',
            type: 'GET',
            dataType: 'JSON',
            async: false,
            success: function (data) {
                //筛选出所有的“参加民生实事项目监督”数据，存入数组msListArray
                var msListArray=[];
                $.each(data.results,function (index,value) {
                    var category=value.category;
                    if (category && category=="参加民生实事项目监督"){
                        msListArray.push(value);
                    }
                });
                //筛选出当前人编号的履职数据存入数组msIdList
                var msIdList=[];
                $.each(msListArray,function (index,val) {
                    var pIdList = new Array();
                    var pId=val.pId;
                    var regex = "、";
                    if (pId!=""&&pId!=undefined){
                        if (typeof pId === 'string'&&pId.indexOf(regex)!=-1){
                            pIdList=pId.split(regex);
                            if (pIdList!=""&&pIdList!=undefined){
                                for(let i=0; i < pIdList.length; i++){
                                    if (pIdList[i]==id){
                                        msIdList.push(val);
                                    }
                                }
                            }else {
                                if (pId==id){
                                    msIdList.push(val);
                                }
                            }
                            }
                    }
                });
                //遍历数据进行展示
                var html="";
                html=`<table cellpadding="0" cellspacing="0" border="0" width="100%" class="table_data ctl">
                  <tr>
                      <th width="40%">标题</th>
                      <th width="10%">时间</th>
                      <th width="30%">参与代表姓名</th>
                      <th width="10%">所属区域</th>
                     
                      <th width="10%">操作</th>
                  </tr>`
                $.each(msIdList,function (index,val1) {
                    liveNum=liveNum+1
                    html=html+`<tr>
                      <td style="text-align: left;padding-left: 8px;" class="">${val1.title}</td>
                      <td class="align_center" >${val1.date}</td>
                      <td style="text-align: left;padding-left: 8px;" class="">${val1.pName}</td>
                      <td class="align_center">${val1.belongPart}</td>
                      
                      <td class="align_center"><a href="javascript:;" onclick="msInfo(${val1.id})" ><i class="icon iconfont icon_chakan "></i></a></td>
                  </tr>`
                });
                var livelihood= html+`</table>`;
                if (liveNum&&liveNum!=0){

                }else {
                    livelihood=html+`<tr><td colspan="6" style=" text-align:center;">暂无数据</td></tr></table>`
                }
                $("#ms_table").html(livelihood);

            }
        })
        $("#liveNum").html(liveNum);
    }
});
function lookInfo(id) {
    console.log(id);
    var urldb='信息详情.html?id='+id;
    window.open(urldb);
}
function info(id) {
    var urldb='代表联系选民.html?id='+id;
    window.open(urldb);
}
function msInfo(id) {
    var urldb='信息详情.html?id='+id;
    window.open(urldb);
}
//履职信息
$(function () {
     page = 1;//页数
     total = 0;//总条数
     totalPage = 0;//总页数
     fId="";
    var id = window.location.href.split("id=")[1]//得到传递过来的代表id
    fId=id;
    resumption(page,id)
    $(function () {
        resumption (page,id);
        pageLoad()
        $("#pre").click(function () {
            if (page == parseInt(total/10)+1){
                $("#next").attr("disabled",false)
            }
            page = page-1;
            if (page<=0){page=1};
            resumption (page,id);
        });
        $("#next").click(function () {
            if (page == 1){
                $("#pre").attr("disabled",false)
            }
            page = page+1;
            if (page > (total/10)+1){page = (total/10)+1};
            resumption (page,id);
        });
    })
})
//履职信息
function resumption(index,id) {
    page=index;
    total=0;
    var list=[];
    sinceNum=0;
    $("#lz_table").html("");
    var html="";
    $.ajax({
        url:'./js/position/position.json',
        type: 'GET',
        dataType: 'JSON',
        async: false,
        success: function (data) {
            list=[];
            html=`<table cellpadding="0" cellspacing="0" border="0" width="100%" class="table_data">
                  <tr>
                      <th width="40%">履职标题</th>
                      <th width="20%">履职日期</th>
                      <th width="30%">履职类型</th>
                      <th width="10%">操作</th>
                  </tr>`;
            $.each(data.results,function (index,val) {
                // var pId=val.pId;
                var pIdList = new Array();
                var pId=val.pId;
                var regex = "、";
                if (pId!=""&&pId!=undefined){
                    if (typeof pId === 'string'&&pId.indexOf(regex)!=-1){
                        pIdList=pId.split(regex);
                        for(let i=0; i < pIdList.length; i++){
                            if (pIdList[i]==id){
                                list.push(val);
                            }
                        }
                    }else {
                        if (pId==id){
                            list.push(val);
                        }
                    }
                }

            });
            $.each(list,function (index,value) {
                sinceNum = sinceNum + 1;
                total = total + 1;
                if (index >= (page - 1) * 10 && index < 10 * page) {
                    html = html + `<tr>
                      <td class="">${value.title}</td>
                      <td class="align_center">${value.date}</td>
                      <td class="align_center">${value.category}</td>
                      <td class="align_center"><a href="javascript:;" onclick="lookInfo(${value.id})" ><i class="icon iconfont icon_chakan "></i></a></td>
                  </tr>`
                }
            })
            totalPage = parseInt(total/10+1);

            if (sinceNum&&sinceNum!=0){
                html=html+`</table>`
            }else {
                html=html+`<tr><td colspan="4" style="text-align:center;">暂无数据</td></tr></table>`
            }
            $("#lz_table").html(html);
            $("#sinceNum").html(sinceNum);
            if (page == 1){
                $("#pre").attr("disabled",true)
            }else {
                $("#pre").attr("disabled",false)
            }
            if (page == parseInt(total/10)+1){
                $("#next").attr("disabled",true)
            }else {
                $("#next").attr("disabled",false)
            };
            pageLoad();
        }
    })
};
function pageLoad() {
    // var dqPage = $("#dqPage").text();//得到当前页数
    // dqPage = parseInt(dqPage);//得到的文本转成int
    // var pageCount = $("#pageCount").text();//得到总页数
    // pageCount = parseInt(pageCount);
    var i = 1;
    i = parseInt(i);
    var item="";
    if (totalPage <= 3 ) {//总页数小于五页，则加载所有页
        for (i; i <= totalPage; i++) {
            if (i == page) {
                item += "<span class='disabled'>"+i+"</span>";
            }else{
                item += '<span class="show" onclick="resumption('+i+',fId)">'+i+'</span>';
            }
        };
        $('#pageBtn').html(item);
        return;
    }else if (totalPage > 3) {//总页数大于五页，则加载五页
        if (page < 3) {//当前页小于5，加载1-5页
            for (i; i <= 3; i++) {
                if (i == page) {
                    item += "<span class='disabled'>"+i+"</span>";
                }else{
                    item += "<span class='show' onclick='resumption("+i+",fId)'>"+i+"</span>";
                }
            };
            if (page <= totalPage-2) {//最后一页追加“...”代表省略的页
                item += "<span> . . . </span>";
            }
            $('#pageBtn').html(item);
            return;
        }else if (page >= 3) {//当前页大于5页
            for (i; i <= 1; i++) {//1,2页码始终显示
                item += "<span class='show' onclick='resumption("+i+",fId)'>"+i+"</span>";
            }
            item += "<span> . . . </span>";//2页码后面用...代替部分未显示的页码
            if (page+1 == totalPage) {//当前页+1等于总页码
                for(i = page-1; i <= totalPage; i++){//“...”后面跟三个页码当前页居中显示
                    if (i == page) {
                        item += "<span class='disabled'>"+i+"</span>";
                    }else{
                        item += "<span class='show' onclick='resumption("+i+",fId)'>"+i+"</span>";
                    }
                }
            }else if (page == totalPage) {//当前页数等于总页数则是最后一页页码显示在最后
                for(i = page-2; i <= totalPage; i++){//...后面跟三个页码当前页居中显示
                    if (i == page) {
                        item += "<span class='disabled'>"+i+"</span>";
                    }else{
                        item += "<span class='show' onclick='resumption("+i+",fId)'>"+i+"</span>";
                    }
                }
            }else{//当前页小于总页数，则最后一页后面跟...
                for(i = page-1; i <= page+1; i++){//page+1页后面...
                    if (i == page) {
                        item += "<span class='disabled'>"+i+"</span>";
                    }else{
                        item += "<span class='show' onclick='resumption("+i+",fId)'>"+i+"</span>";
                    }
                }
                item += "<span> . . . </span>";
            }
            $('#pageBtn').html(item);
            return;
        }
        // console.log(item);
    }
};

